#include "SchedulerInc.h"

using namespace bimar_scheduler_lib;

CJobsWorkerFollow::CJobsWorkerFollow(gulong maxExecutors)
:m_maxNumExecutors(maxExecutors)
{
}

gboolean CJobsWorkerFollow::Create()
{
  CGAutoLock lock(&m_jobExecCS);
  
  for(gulong i=0; i<m_maxNumExecutors; i++)
    m_jobExec.push_back(new CJobExecutor());

  return TRUE;
}

CJobExecutor * CJobsWorkerFollow::GetFirstIdleExecutor()
{
  CGAutoLock lock(&m_jobExecCS);
  
  for(JobExecutorIt it=m_jobExec.begin(); it!=m_jobExec.end(); it++)
  {
	CJobExecutor *exec = *it;
	if(exec && exec->IsIdle())
      return exec;
  }

  return NULL;
}

gulong CJobsWorkerFollow::RunThread()
{
  while(m_running)
  {
    CJobExecutor *exec = GetFirstIdleExecutor();
	if(exec)
	{
	  CJob *job = GetNextJobToExecute();
	  if(job)
	    StartJob(exec, job);
	}
	g_thread_yield();
  }

  return 0x009;
}
